package com.only4play.pcc.controller;

import com.only4play.pcc.domain.User;
import com.only4play.pcc.rest.GenericResponse;
import com.only4play.pcc.service.LikeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liyuncong
 * @version 1.0
 * @file FeedController
 * @brief FeedController
 * @details FeedController
 * @date 2024-06-24
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-06-24               liyuncong          Created
 */

@Slf4j
@RestController
@RequestMapping("/pcc")
public class PCCController {

    private static final int DEFAULT_LIMIT = 20;

    @Resource
    public LikeService likeService;


    @PutMapping(params = "action=like")
    public GenericResponse<?> like(
        @RequestParam("oid") long oid,
        @RequestParam("uid") long uid
    ) {
        // query had liked
        Long like = likeService.islike(oid, uid);
        if (like == 1) {
            Map<String, Object> payload = new HashMap<>(2);
            payload.put("oid", oid);
            payload.put("uid", uid);
            return GenericResponse.failure("object already been liked.", payload);
        }

        // do like
        Long doLike = likeService.like(oid, uid);
        if (doLike > 0) {
            Map<String, Object> payload = new HashMap<>(3);
            payload.put("oid", oid);
            payload.put("uid", uid);
            // query the first 20 user id and name
            List<User> users = likeService.queryLatestLikeUsers(oid, DEFAULT_LIMIT);

            payload.put("like_list", users);
            return GenericResponse.success(payload);
        }

        return GenericResponse.failure("like failure, please try again.");
    }

    @DeleteMapping(params = "action=unlike")
    private GenericResponse<?> unlike(
        @RequestParam("oid") long oid,
        @RequestParam("uid") long uid
    ) {
        // query had liked
        Long like = likeService.islike(oid, uid);
        if (like == 0) {
            Map<String, Object> payload = new HashMap<>(2);
            payload.put("oid", oid);
            payload.put("uid", uid);
            return GenericResponse.failure("object has not been liked yet.", payload);
        }

        // do unlike
        Long unlike = likeService.unlike(oid, uid);
        if (unlike > 0) {
            Map<String, Object> payload = new HashMap<>(3);
            payload.put("oid", oid);
            payload.put("uid", uid);
            // query the first 20 user id and name
            List<User> users = likeService.queryLatestLikeUsers(oid, DEFAULT_LIMIT);

            payload.put("like_list", users);
            return GenericResponse.success(payload);
        }
        return GenericResponse.failure("like failure, please try again.");
    }

    @GetMapping(params = "action=is_like")
    private GenericResponse<?> isLike(
        @RequestParam("oid") long oid,
        @RequestParam("uid") long uid
    ) {
        // query had liked
        Long isLiked = likeService.islike(oid, uid);

        Map<String, Object> payload = new HashMap<>(3);
        payload.put("oid", oid);
        payload.put("uid", uid);
        payload.put("is_like", isLiked);

        return GenericResponse.success(payload);
    }

    @GetMapping(params = "action=count")
    private GenericResponse<?> count(
        @RequestParam("oid") long oid
    ) {
        // query count
        Long count = likeService.count(oid);

        Map<String, Object> payload = new HashMap<>();
        payload.put("oid", oid);
        payload.put("count", count);

        return GenericResponse.success(payload);
    }

    /**
     * @param cursor   last returned result cursor
     * @param limit    default 20
     * @param isFriend 1: is friend ; 0 not friend
     * @return
     */
    @GetMapping(params = "action=list")
    private GenericResponse<?> list(
        @RequestParam("oid") Long oid,
        @RequestParam(value = "uid", required = false) Long uid,
        @RequestParam(value = "cursor", required = false) Long cursor,
        @RequestParam(value = "limit", defaultValue = "20", required = false) Integer limit,
        @RequestParam(value = "is_friend", defaultValue = "0", required = false) Integer isFriend
    ) {
        // if current uid null, friends list will not be prioritized
        if (null == uid) {
            isFriend = 0;
        }

        // if cursor empty, default from 0
        if (null == cursor) {
            cursor = 0L;
        }

        // limit size
        if (null == limit || limit <= 0 || limit > 100) {
            limit = DEFAULT_LIMIT;
        }

        // isFriend value is wrong, default set 0
        if (isFriend != 0 && isFriend != 1) {
            isFriend = 0;
        }

        Map<String, Object> payload = new HashMap<>(4);
        payload.put("oid", oid);
        payload.put("uid", uid);

        List<User> users = likeService.list(oid, uid, cursor, limit, isFriend);
        Long nextCursor = null;
        if (!CollectionUtils.isEmpty(users)) {
            if (users.size() > limit) {
                nextCursor = users.get(users.size() - 1).getUid();
            }
        }

        payload.put("like_list", users);
        payload.put("next_cursor", nextCursor);

        return GenericResponse.success(payload);
    }


}
